package com.atguigu.gmall.rabbit.configuration;

import com.atguigu.gmall.rabbit.constant.RabbitConstant;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OrderDelayQueueConfiguration {

    @Bean
    public Exchange exchange() {
        // durable方法参数设置为true表示交换机需要进行持久化
        Exchange exchange = ExchangeBuilder.directExchange(RabbitConstant.ORDER_EXCHANGE).durable(true).build();
        return exchange ;
    }

    @Bean
    public Queue orderQueue() {
        Queue queue = QueueBuilder.durable(RabbitConstant.ORDER_QUEUE)
                .deadLetterExchange(RabbitConstant.ORDER_EXCHANGE)
                .deadLetterRoutingKey(RabbitConstant.DEAD_LETTER_ROUTING_KEY)
                .ttl(1000 * 60 * 30)
                .build();
        return queue ;
    }

    @Bean
    public Binding orderQueueBinding() {
        Binding binding = BindingBuilder.bind(orderQueue()).to(exchange()).with(RabbitConstant.ORDER_ROUTING_KEY).noargs();
        return binding;
    }

    @Bean
    public Queue orderDelayQueue() {
        Queue queue = QueueBuilder.durable(RabbitConstant.ORDER_DELAY_QUEUE).build();
        return queue ;
    }

    @Bean
    public Binding orderDelayQueueBinding() {
        Binding binding = BindingBuilder.bind(orderDelayQueue()).to(exchange()).with(RabbitConstant.DEAD_LETTER_ROUTING_KEY).noargs();
        return binding ;
    }

    @Bean
    public Queue orderPayedQueue() {
        Queue queue = QueueBuilder.durable(RabbitConstant.ORDER_PAYED_QUEUE).build();
        return queue ;
    }

    @Bean
    public Binding orderPayedQueueBinding() {
        Binding binding = BindingBuilder.bind(orderPayedQueue()).to(exchange()).with(RabbitConstant.ORDER_PAYED_ROUTING_KEY).noargs();
        return binding ;
    }

}
